package com.roger.tool.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;

public class AppInfoUtil {
	
	public static String getAppName(Context pContext) {
		try {
			PackageInfo pkg = pContext.getPackageManager().getPackageInfo(pContext.getPackageName(), 0);
			String appName = pkg.applicationInfo.loadLabel(pContext.getPackageManager()).toString();
			return appName;
		} catch (Exception e) {
			
		}
		return "error";
	}
	
	/**
	 * 获取应用程序版本名称信息
	 * @param context
	 * @return 当前应用的版本名称
	 */
	public static String getVersionName(Context context) {
		try {
			PackageManager packageManager = context.getPackageManager();
			PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
			return packageInfo.versionName;

		} catch (NameNotFoundException e) {
			
		}
		return null;
	}
	
	public static int getVersionCode(Context context) {
		try {
			PackageInfo pinfo = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_CONFIGURATIONS);
			return pinfo.versionCode;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}
	
	public static int getResourceId(Context context, String name, String type) {
		try {
			String packageName = context.getPackageName();
			return context.getResources().getIdentifier(name, type, packageName);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

}
